require 'aker'

module Aker::Form::Middleware
  class LogoutResponder
    include Aker::Form::LoginFormAssetProvider
    include Aker::Rack::ConfigurationHelper

    def initialize(app)
      @app = app
    end

    ##
    # When given `GET` to the configured logout path, builds a Rack
    # response containing the login form with a "you have been logged
    # out" notification.  Otherwise, passes the response on.
    #
    # @return a finished Rack response
    def call(env)
      if env['REQUEST_METHOD'] == 'GET' && env['PATH_INFO'] == logout_path(env)
        ::Rack::Response.new(login_html(env, :logged_out => true)) do |resp|
          resp['Content-Type'] = 'text/html'
        end.finish
      else
        @app.call(env)
      end
    end
  end
end
